package com.singfan.manage;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Author: hansonghe
 * Date: 2015-10-12 16:45
 * Desc:
 */
@Component
public class AuthInterceptor extends HandlerInterceptorAdapter {
    private static final String ADMIN_SESSION_KEY = "ADMIN_KEY";
    private static final String LOGIN_PATH = "/login";
    private static final String LOGIN_SUBMIT_PATH = "/login/submit";
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        boolean result = true;

        if (!request.getRequestURI().equals(LOGIN_PATH) && !request.getRequestURI().equals(LOGIN_SUBMIT_PATH) && request.getSession().getAttribute(ADMIN_SESSION_KEY) == null){
            response.sendRedirect(LOGIN_PATH);

            result = false;
        }

        return result;
    }
}
